using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;

namespace Sanchay
{
    public static class Validation
    {

        //public static TractorPurchase sVarTractorPurchase = null;

        /// <method>
        /// Method for Empty Check in  controls on the Form
        /// </method>
        /// <param name="ctrl">Control Array for which Empty Validation check is required</param>
        public static bool CheckEmpty(Control c)
        {
            //foreach (Control c in ctrl)
            //{

            //    if (c is TextBox)
            //    {
            //        TextBox tb = (TextBox)c;
            //        if (tb.Text == "")
            //        {
            //            tb.BackColor = Color.Pink;
            //            return true;
            //        }

            //        else
            //        {
            //            tb.BackColor = Color.White;
            //            return false;
            //        }

            //    }

            //    else if (c is ComboBox)
            //    {
            //        ComboBox cb = (ComboBox)c;
            //        if (c.Text == "")
            //        {
            //            c.BackColor = Color.Pink;
            //            return true;
            //        }
            //        else
            //        {
            //            c.BackColor = Color.White;
            //            return false;
            //        }

            //    }




            //    }

            if (c is TextBox)
            {
                TextBox tb = (TextBox)c;
                if (tb.Text == "")
                {
                    tb.BackColor = Color.Pink;
                    return true;
                }

                else
                {
                    tb.BackColor = Color.White;
                    return false;
                }



            }

            else if (c is ComboBox)
            {
                ComboBox cb = (ComboBox)c;
                if (cb.SelectedIndex == -1)
                {
                    cb.BackColor = Color.Pink;

                    return true;
                }
                else
                {
                    cb.BackColor = Color.White;
                    return false;
                }

            }
            return false;
        }

        public static bool ValidateEmpty(Control[] ctrl)
        {

            int validCount = 0;
            foreach (Control c in ctrl)
            {

                if (c is TextBox)
                {
                    TextBox tb = (TextBox)c;
                    if (tb.Text == "")
                    {
                        tb.BackColor = Color.Pink;

                    }

                    else
                    {
                        tb.BackColor = Color.White;
                        validCount += 1;
                    }

                }

                else if (c is ComboBox)
                {
                    ComboBox cb = (ComboBox)c;
                    if (c.Text == "")
                    {
                        c.BackColor = Color.Pink;

                    }
                    else
                    {
                        c.BackColor = Color.White;
                        validCount += 1;
                    }

                }

            }
            if (validCount == ctrl.Length)
                return true;
            else
                return false;

        }

        public static void CheckDataGridViewEmptyCell(DataGridView dgv, string[] Columns)
        {
            int count1 = 0;
            for (int i = 0; i < dgv.Rows.Count - 1; i++)
            {
                if (dgv.Rows[i].Cells[0].Value == null)
                {
                    dgv.Rows[i].Cells[0].Style.BackColor = Color.Pink;
                    count1 += 1;
                }
                else
                {
                    dgv.Rows[i].Cells[0].Style.BackColor = Color.White;
                }


                // Model Cell
                if (dgv.Rows[i].Cells[0].Value == null)
                {
                    dgv.Rows[i].Cells[0].Style.BackColor = Color.Pink;
                    count1 += 1;
                }
                else
                {
                    dgv.Rows[i].Cells[0].Style.BackColor = Color.White;
                }
                // Serial No. Cell
                if (dgv.Rows[i].Cells[1].Value == null)
                {
                    dgv.Rows[i].Cells[1].Style.BackColor = Color.Pink;
                    count1 += 1;
                }
                else
                {
                    dgv.Rows[i].Cells[1].Style.BackColor = Color.White;
                }
                // Engine No. Cell
                if (dgv.Rows[i].Cells[2].Value == null)
                {
                    dgv.Rows[i].Cells[2].Style.BackColor = Color.Pink;
                    count1 += 1;
                }
                else
                {
                    dgv.Rows[i].Cells[2].Style.BackColor = Color.White;
                }
            }
        }// ends For loop

        /// <method>
        /// Method for Resetting all controls on the Form
        /// </method>
        /// <param name="form"></param>
        public static void ResetFields(Control form)
        {
            foreach (Control ctrl in form.Controls)
            {
                if (ctrl.Controls.Count > 0)
                    ResetFields(ctrl);
                Reset(ctrl);
            }
        }
        private static void Reset(Control ctrl)
        {
            TextBox cCtrl = ctrl as TextBox;
            //TextBox tb = (TextBox)ctrl;
            if (cCtrl != null)
            {
                cCtrl.ResetText();
                cCtrl.BackColor = Color.White;
                return;
            }

            ComboBox cbCtrl = ctrl as ComboBox;

            if (cbCtrl != null)
            {
                cbCtrl.SelectedIndex = -1;
                cbCtrl.BackColor = Color.White;
                return;
            }

            DataGridView dgvCtrl = ctrl as DataGridView;

            if (dgvCtrl != null)
            {
                dgvCtrl.Rows.Clear();
                dgvCtrl.Rows.Add();
                return;
            }

        }

        public static void ToCurrencyFormat(TextBox[] text)
        {
            if (text != null)
            {


                foreach (TextBox TB in text)
                {
                    TB.Enter += new System.EventHandler(Enter);

                    TB.KeyPress += new System.Windows.Forms.KeyPressEventHandler(KeyPress);
                    TB.Leave += new System.EventHandler(Leave);
                }


            }
        }
        private static void Enter(object sender, EventArgs e)
        {
            TextBox TBB = sender as TextBox;
            if (TBB.Text == "")
            {
                return;

            }
            //TBB.Text = TBB.Text.Replace(",", "*").Replace(".", ",").Replace("*", ".");
            string mytext = TBB.Text;
            foreach (Char chr in mytext)
            {
                if ((!char.IsDigit(chr) && (chr.ToString() != ".")))
                {
                    mytext = mytext.Replace(chr.ToString(), "");
                }
            }
            TBB.Text = mytext;
        }

        private static void KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox TBB = sender as TextBox;
            //base.OnKeyPress(e);
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar.ToString() != ".")
            {
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar.ToString() == "." && TBB.Text.Contains("."))
                {
                    e.Handled = true;
                }
            }
        }
        // public string SFORMAT = "##,##0.#0";
        private static void Leave(object sender, EventArgs e)
        {
            TextBox TBB = sender as TextBox;
            if (TBB.Text == "")
            {
                //TBB.Text = "0";
                return;
            }
            NumberFormatInfo nfi = CultureInfo.GetCultureInfo("hi-IN").NumberFormat;
            nfi = (NumberFormatInfo)nfi.Clone();
            nfi.CurrencySymbol = "";
            nfi.CurrencyPositivePattern = 0;
            //TBB.Text = string.Format("{0:0,0.00}", decimal.Parse(TBB.Text));
            //TBB.Text = Strings.Format(Conversion.Val(TBB.Text), "###,###.00");
            //TBB.Text = TBB.Text.Replace(",", "*").Replace(".", ",").Replace("*", ".");
            TBB.Text = string.Format(nfi, "{0:c}", double.Parse(TBB.Text));
            //TBB.Text = TBB.Text.Replace(",", "*").Replace(".", ",").Replace("*", ".");

        }

        /// <method>
        /// Putting zeros if data field type is numeric and textbox is empty to avoid exception
        /// </method>
        /// <param name="t"></param>
        public static void CheckEmptyNumeric(TextBox[] tb)
        {
            foreach (TextBox t in tb)
            {
                if (t.Text == "")
                {
                    t.Text = "0";
                }
            }

        }



        public static void showForm(Form targetForm)
        {
            if (targetForm != null)
            {
                targetForm.Activate();
            }
            else
            {
                targetForm = new Form();

            }
        }

        public static bool CheckIfNumeric(object p)
        {
            decimal decimalVal;
            try
            {
                if (p != null)
                {
                    decimalVal = Convert.ToDecimal(p);
                    return true;
                }

                if (Convert.ToDecimal(p) == 0)
                {
                    return false;
                }
                return false;
            }
            catch (System.OverflowException)
            {
                return false;
            }
            catch (System.FormatException)
            {
                return false;
            }
            catch (System.ArgumentNullException)
            {
                return false;
            }

            //try
            //{
            //    Convert.ToDecimal(p);
            //    return true;
            //}
            //catch
            //{
            //    return false;
            //}

        }

        public static bool CheckIfEmpty(object p)
        {

            if (Convert.ToString(p) == "")
                return true;
            return false;
        }

        public static bool CheckIfNull(object p)
        {
            if (p == null)
                return true;
            return false;
        }
    }

}

